<?php
class EmployeesController extends AppController {

	var $name = 'Employees';

	function index() {
		$this->Employee->recursive = 0;
		$this->set('employees', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('employee', $this->Employee->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Employee->create();
			if ($this->Employee->save($this->data)) {
				$this->Session->setFlash(__('The employee has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Employee->save($this->data)) {
				$this->Session->setFlash(__('The employee has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Employee->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for employee', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Employee->delete($id)) {
			$this->Session->setFlash(__('Employee deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Employee was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        function complete($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		if (empty($this->data)) {
                    $sendit;
                    $this->Employee->recursive = 2;
                    $this->data = $this->Employee->read(null, $id);
                    $this->data['Employee']['complete'] = 1;
			if ($this->Employee->save($this->data)) {
				$this->Session->setFlash(__('The employee has completed and a IT Ticket was generated', true));
				//$this->redirect(array('action' => 'index'));
                                //debug($this->data);

                                // Lese Benutzer aus
                                $sendit['Benutzername'] = $this->data['Employeedetail']['0']['login'];
                                //Braucht Telefon?
                                if($this->data['Employeedetail']['0']['phone']){
                                    $sendit['Telefon'] = "JA";
                                }else{
                                     $sendit['Telefon'] = "NEIN";
                                }
                                //Braucht Handy?
                                if($this->data['Employeedetail']['0']['mobilephone']){
                                    $sendit['Handy'] = "JA";
                                }else{
                                     $sendit['Handy'] = "NEIN";
                                }
                                //Braucht UMTS?
                                if($this->data['Employeedetail']['0']['umts']){
                                    $sendit['UMTS'] = "JA";
                                }else{
                                     $sendit['UMTS'] = "NEIN";
                                }
                                $sendit['Platz'] = $this->data['Employeedetail']['0']['seat'];
                                $sendit['Abteilung'] = $this->data['Employeedetail']['0']['Department']['name'];
                                $sendit['Titel'] = $this->data['Employeedetail']['0']['Degree']['name'];
                                $sendit['Anstellung'] = $this->data['Employeedetail']['0']['Contract']['name'];
                                $sendit['Hardware'] = $this->data['Employeedetail']['0']['Hardware']['name'];
                                $sendit['Mailinglisten'] = null;
                                foreach ($this->data['Employeedetail']['0']['Mailinglist'] as $mailinglist){
                                    if($sendit['Mailinglisten'] == null)
                                    {
                                     $sendit['Mailinglisten'] = $mailinglist['name'];
                                    } else {
                                     $sendit['Mailinglisten'] = $sendit['Mailinglisten'] . ", " . $mailinglist['name'];
                                    }
                                }
                                $sendit['SAPSysteme'] = null;
                                foreach ($this->data['Employeedetail']['0']['Sapserver'] as $sapserver) {
                                    if($sendit['SAPSysteme'] == null) {
                                        $sendit['SAPSysteme'] =  $sapserver['name'];
                                    } else {
                                        $sendit['SAPSysteme'] =  $sendit['SAPSysteme'] . ", " . $sapserver['name'];
                                    }
                                }
                                //$this->Email->template = 'simple_message';
                                $this->set('Infos', $sendit);


                        // Ende bei erfolgreichem Speichern
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true));
			}
                        //@TODO Sende eine Email ans OTRS
                        

		}
	}
}
?>